package com.j180.mybatisplus_2demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j180.mybatisplus_2demo.bean.StudentBean;
import com.j180.mybatisplus_2demo.dao.IStudentDao;
import com.j180.mybatisplus_2demo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<IStudentDao,StudentBean>  implements  IStudentService {

    @Autowired
    private IStudentDao iStudentDao;

    @Override
    public int addStudentInfo(StudentBean studentBean) {
        return iStudentDao.addStudent(studentBean);
    }

    @Override
    public IPage showStudentInfoByPage(Map<String, String> condition) {

        String curenpage = condition.get("curpage");
        String pageSize = condition.get("pageSize");
        String name = condition.get("stduentName");
        String gender = condition.get("stduentGender");
        String className = condition.get("className");
        //创建分页
        Page<StudentBean> page = new Page<>(Integer.parseInt(curenpage),Integer.parseInt(pageSize));
        //创建一个条件查询器
        QueryWrapper wrapper = new QueryWrapper();

        if(!"".equals(name)&&name!=null){
            wrapper.like("stu.name",name);
        }
        if(!"".equals(gender)&&gender!=null){
           wrapper.eq("stu.gender",gender);
        }
        if(!"".equals(className)&&className!=null){
            wrapper.like("cls.name",className);
        }
        wrapper.orderByAsc("stu.id");

        IPage iPage = iStudentDao.getAllStudents(page,wrapper);

        return iPage;
    }
}
